﻿

var lgtpl = {
    tpls: new Array(),
    initTpl: function () {
        $(".lgtpl").each(function () {
            var i = $(this).index(".lgtpl");
            lgtpl.tpls[i] = $(this).html();
            $(this).html("")
        })
    },
    renderItem: function (d, t) {
        t = $(t);
        if (t.length <= 0) { return }
        var index = t.index(".lgtpl")
        var html = lgtpl.tpls[index];
        var item = html.match(/{@d\.(\w.+?)}/g);//接口返回的对像中的list
        for (var i = 0; i < item.length; i++) {
            var key = item[i].replace(/{@d\.(\w.+?)}/g, "$1")
            html = html.replace(item[i], d[key])
        }
        t.html("").html(html);
    },
    renderList: function (d, t) {
        t = $(t);
        if (t.length <= 0) { return }
        var index = t.index(".lgtpl")
        var tpl = lgtpl.tpls[index];
        var list = tpl.match(/{@list\.(\w.+?)}/g);//接口返回的对像
        var html = "";
        for (var i = 0; i < d.length; i++) {
            var data = d[i];
            html += tpl;
            for (var j = 0; j < list.length; j++) {
                var key = list[j].replace(/{@list\.(\w.+?)}/g, "$1")
                html = html.replace(list[j], data[key])
            }
        }
        t.html("").html(html);
    }

}
